#include "exportdatawidget.h"
#include "ui_exportdatawidget.h"
#include "QFileDialog"
#include "qdebug.h"
#include "Core/ExcelTool/exceldao.h"
#include "QFile"
#include "QDir"
#include "QMessageBox"
#include "Core/ADO/goodinfodao.h"
#include "Core/ADO/goodinfo.h"
#include "QVariant"

ExportDataWidget::ExportDataWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ExportDataWidget)
{
    ui->setupUi(this);
    this->setFixedSize(this->width(),this->height());
}

ExportDataWidget::~ExportDataWidget()
{
    delete ui;
}

void ExportDataWidget::on_choose_dir_btn_clicked()
{
    QString dir = QFileDialog::getExistingDirectory(this, tr("选择导出目录"), "/home", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    this->ui->dir_lab->setText(dir);
}

void ExportDataWidget::on_export_btn_clicked()
{
    QString dir_path = this->ui->dir_lab->text();
    QFileInfo file_info(dir_path);
    if(file_info.exists())
    {
        // 将所有数据都导出Excel
        Datas_List *datas_list = new Datas_List();
        auto good_info_list = GoodInfoDao::GetInstance()->ReadFullTableData();

        Datas_List_Item_List *list_item = new Datas_List_Item_List();

        // 标题
        QVariant *_val_id = new QVariant("商品编号");
        list_item->push_back(*_val_id);

        QVariant *_val_name = new QVariant("商品名称");
        list_item->push_back(*_val_name);

        QVariant *_val_cost_ = new QVariant("进货价格");
        list_item->push_back(*_val_cost_);

        QVariant *_val_sale = new QVariant(QString("卖出价格") );
        list_item->push_back(*_val_sale);

        QVariant *_val_inventory = new QVariant("剩余库存");
        list_item->push_back(*_val_inventory);

        QVariant *_val_des = new QVariant("图片路径");
        list_item->push_back(*_val_des);

        QVariant *_val_img = new QVariant("商品描述");
        list_item->push_back(*_val_img);

        datas_list->push_back(*list_item);

        for(GoodInfo info : good_info_list)
        {
            Datas_List_Item_List *list_item = new Datas_List_Item_List();

            QVariant *val_id = new QVariant(info.good_id);
            list_item->push_back(*val_id);

            QVariant *val_name = new QVariant(info.good_name);
            list_item->push_back(*val_name);

            QVariant *val_cost_ = new QVariant(info.cost_price);
            list_item->push_back(*val_cost_);

            QVariant *val_sale = new QVariant(info.sale_price);
            list_item->push_back(*val_sale);

            QVariant *val_inventory = new QVariant(info.inventory);
            list_item->push_back(*val_inventory);

            QVariant *_val_img = new QVariant(info.img_name);
            list_item->push_back(*_val_img);

            QVariant *val_des = new QVariant(info.description);
            list_item->push_back(*val_des);

            datas_list->push_back(*list_item);
        }

        qDebug()<< QString("on_export_btn_clicked  datas_list->size : %1").arg(QString::number(datas_list->size()));
        qDebug()<< QString("on_export_btn_clicked  good_info_list.size : %1").arg(QString::number(good_info_list.size()));

        this->setEnabled(false);
        ExcelDao::ExportToExcel(dir_path,"GoodInfoDBTable",*datas_list);
        this->setEnabled(true);

        QMessageBox box(QMessageBox::Warning,"导出","导出成功"); //,
        box.setStandardButtons (QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok,QString("确定"));
        box.setFixedSize(400,250);
        box.exec();
    }
    else
    {
        QMessageBox box(QMessageBox::Warning,"错误","选择输入路径有误"); //,
        box.setStandardButtons (QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok,QString("确定"));
        box.setFixedSize(400,250);
        box.exec();
    }
    this->ui->dir_lab->setText("");
}

void ExportDataWidget::on_close_btn_clicked()
{
    this->close();
}
